//
//  UIBarButtonItem+Extension.swift
//  weibo
//
//  Created by 王向宁 on 16/5/19.
//  Copyright © 2016年 王向宁. All rights reserved.
//

import UIKit

extension UIBarButtonItem {
    convenience init(imageName: String? = nil,title: String? = nil,target: AnyObject?,action: Selector) {
        self.init()
        
        let btn = UIButton()
        
        if let ima = imageName {
            btn.setImage(UIImage(named: ima), forState: .Normal)
            btn.setImage(UIImage(named: ima + "_highlighted"), forState: .Highlighted)
        }
        
        
        btn.addTarget(target, action: action, forControlEvents: .TouchUpInside)
        
        btn.setTitle(title, forState: .Normal)
        
        //设置图片的显示范围
        btn.imageEdgeInsets = UIEdgeInsets(top: 0, left: -8, bottom: 0, right: 0)
        
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Highlighted)
        btn.sizeToFit()
        
        self.customView = btn
    }
}
